/*
 * $Id$
 */
package corejs.rt;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author myang
 */
public class ActivationRecord 
   extends HashMap<String, Value> implements NameResolver<String, Value> {

   private ActivationRecord _parent;
   private Value _return;
   
   /**
    *
    * @param init
    */
   public ActivationRecord(final Map<String, Value> init) {
      this(null, init);
   }


   /**
    * 
    * @param parent 
    */
   public ActivationRecord(final ActivationRecord parent) {
      this(parent, Collections.<String, Value>emptyMap());
   }

   /**
    * 
    * @param parent
    * @param init 
    */
   public ActivationRecord(final ActivationRecord parent, 
                           final Map<String, Value> init) {
      for (final Map.Entry<String, Value> e : init.entrySet()) {
         put(e.getKey(), e.getValue());
      }     
      _parent = parent;
   }

   @Override
   public Value resolve(final String name) {
      final Value o = get(name);
      return null != o   ? o
           : hasParent() ? _parent.resolve(name)
           :               get("this").resolve(name);
   }

   @Override
   public Value store(final String name, final Value val) {
      put(name, val);
      return val;
   }

   public ActivationRecord setReturn(final Value rt) {
      _return = rt;
      return this;
   }
   
   public Value getReturn() {
      return _return;
   }
   
   public ActivationRecord parent() {
      return _parent;
   }
   
   public boolean hasParent() {
      return null != _parent;
   }

}
